<template>
    <web 
        ref="webview"
        :style="contentStyle"
        :src="url"
        @pagestart="onPageStart" 
    >
    </web>
</template>
<script>
const modal = weex.requireModule("modal");
const storage = weex.requireModule('storage')
const navigator = weex.requireModule('navigator')

import { Utils } from "weex-ui"
import { getBaseUrl, serialize, jsonAnalysis, getUserInfoModel } from "@/utils/index.js"
let recharge = new BroadcastChannel('app')

export default {
    data() {
        return {
            url:'',
            contentStyle:{},
            money:'',
            baseURL:'',
            type:''
        }
    },

    methods:{
        getCacheMark(name) {
			return new Promise((resolve, rejects) => {
				storage.getItem(name, event => {
					if (event.result === "success") {
						resolve(JSON.parse(event.data));
					} else {
						rejects("获取缓存数据失败!");
					}
				});
			})
        },

        onPageStart(event) {
            // 查询资金并缓存资金信息
            getUserInfoModel()
            
            if(event.url.indexOf('gcPayStatus') > -1 && !this.type) {
                recharge.postMessage('post')
                navigator.pop()
            }

            if(this.type == 'pay' && event.url.indexOf('gcPayStatus') > -1) {
                navigator.push({
                    url: this.baseURL + 'views/pay/paysuccess.js',
                    animated: "true"
                })
            }

            
        }
    },

    created() {

        const tabPageHeight = Utils.env.getPageHeight()
        this.baseURL = getBaseUrl(weex.config.bundleUrl, true);

        
        this.getCacheMark('webviewUrl').then(value=>{
            this.url = value
        })

        let { money, type } = jsonAnalysis(weex.config.bundleUrl.split("?")[1])


		this.money = money
        this.type = type || ''

        this.contentStyle = {
            height: tabPageHeight + "px",
            width: "750px"
		}
    }
}
</script>
